package com.itheima.service.impl;

import com.itheima.dao.MenuDao;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
//查询所有的菜单
    @Override
    public List<Menu> findAllMenu() {
        //查询所有的菜单数据
        List<Menu> menuList=menuDao.findAllMenu();
        //封装数据
        List<Menu> resultMenuList=bulidMenu(menuList);
        return resultMenuList;
    }
    //添加一级菜单
    @Override
    public void addOneMenu(Menu menu) {
        menuDao.addOneMenu(menu);
    }
    //添加二级菜单
    @Override
    public void addTwoMenu(Menu menu) {
        menuDao.addTwoMenu(menu);
    }
    //修改菜单
    @Override
    public void EditMenu(Menu menu) {
        menuDao.EditMenu(menu);
    }
//删除菜单
    @Override
    public void deleteMenuById(Integer id) {
       //将子菜单和本菜单一起删除
       List<Integer> deleteIdList=new ArrayList<>();
       deleteIdList= deleteMenuChildrenById(id,deleteIdList);
       deleteIdList.add(id);
       //1.删除本菜单和子菜单
        menuDao.deleteIdList(deleteIdList);
      //2.删除角色和菜单中间表(其中包括菜单中的子菜单的中间表)
        menuDao.deleteRoleIdAndMenuIdByMenuIdList(deleteIdList);
    }
//向list中添加要删除的子菜单id
    public List<Integer> deleteMenuChildrenById(Integer id, List<Integer> deleteIdList) {
        //查询所有的菜单
        List<Menu> allMenu = menuDao.findAllMenu();
        //pid等于此菜单id就是此菜单的子菜单
        allMenu.stream().filter(menu -> menu.getParentMenuId()==id).map(menu ->{
            deleteIdList.add(menu.getId());
            deleteMenuChildrenById(menu.getId(),deleteIdList);
            return deleteIdList; }
        );
        System.out.println("要删除的子菜单id"+deleteIdList);
        return deleteIdList;
    }

    //封装数据
    public List<Menu> bulidMenu(List<Menu> menuList) {
        //创建集合用来封装数据并返回
        List<Menu> finallyMenuList =new ArrayList<>();
        //遍历所有菜单集合,找到最外层的菜单(所有数据菜单即pid=null并且level=0)
        finallyMenuList=menuList.stream().filter(menu ->menu.getLevel()==0).map(menu->{
            System.out.println("root菜单"+menu);
            menu.setChildren(getChildren(menu,menuList));
            return menu;
        }).collect(Collectors.toList());
        return finallyMenuList;
    }

    //封装最外层菜单的子菜单
    public List<Menu> getChildren(Menu root,List<Menu> all ){
        //查找主菜单下的子菜单(就是当前菜单的id=子菜单的pid)
     List<Menu> children= all.stream().filter(menu -> menu.getParentMenuId()==root.getId()).map(menu -> {
            //找到了子菜单将它封装到夫菜单的children(子菜单还有子菜单(递归))
         System.out.println("找到的子菜单"+menu);
            menu.setChildren(getChildren(menu,all));
         System.out.println("封装好的子菜单"+menu);
            return menu;
        }).collect(Collectors.toList());
     return children;
    }
}
